#include "SaveWindow.h"

SaveWindow::SaveWindow(QDialog *parent) : QDialog(parent){
	setupUi(this);
	this->videoListWidget->addItem("avi");
	this->videoListWidget->addItem("mpg");
	this->videoListWidget->addItem("dv");
	this->videoListWidget->setCurrentRow(0);
	this->audioListWidget->addItem("mp3");
	this->audioListWidget->addItem("mp2");
	this->audioListWidget->addItem("ogg");
	this->audioListWidget->setCurrentRow(0);
	this->connectSlots();
}

SaveWindow::~SaveWindow(){
}

void SaveWindow::connectSlots(){
	connect(outFileNameButton, SIGNAL(clicked()), this, SLOT(selectFileName()));
	connect(this, SIGNAL(accepted()), this, SLOT(save()));
}

void SaveWindow::save(){
	Mlt::Profile profile("square_ntsc_wide");
	Mlt::Consumer *outFileConsumer; 
	int videoChoice = 0;
	QString fileName = getFileName();
	QString extension;
	if(formatComboBox->currentIndex() == videoChoice){
		extension = videoListWidget->selectedItems()[0]->text();
		if(!fileName.endsWith("." + extension)){
			fileName += "." + extension;
		}
		outFileConsumer = new Mlt::Consumer (profile, "avformat", fileName.toUtf8().constData());
	}else{
		extension = audioListWidget->selectedItems()[0]->text();
		if(!fileName.endsWith("." + extension)){
			fileName += "." + extension;
		}
		outFileConsumer = new Mlt::Consumer (profile, "sdl_audio", getFileName().toUtf8().constData());
	}
	Mlt::Playlist *tmpPlaylist = new Mlt::Playlist();
	tmpPlaylist->append(*playlist);
	outFileConsumer->connect(*tmpPlaylist);
	outFileConsumer->run();
	QMessageBox::information(this, "Fichier", "Fichier enregistre avec succes !");
}

void SaveWindow::selectFileName(){
	QString fileName = QFileDialog::getSaveFileName(this, "Enregistrer les extractions", QString(), "Video (*.avi *.mpg *.dv) Audio (*.mp3 *.mp2 *.ogg) all (*.*)");
	if(!fileName.isNull()){
		this->outFileNameLineEdit->setText(fileName);
	}
}

QString SaveWindow::getFileName(){
	return this->outFileNameLineEdit->text();
}

void SaveWindow::setPlaylist(Mlt::Playlist *playlist){
	this->playlist = playlist;
}

